package com.shen.mianshi.xinde;

import java.util.concurrent.BlockingQueue;

/**
 * 消费者
 * @author Administrator
 *
 */
public class Consumer implements Runnable{

	private BlockingQueue<Message> queue;
	
	private HandleA ha ;
	
	private HandleB hb ;
	
	public Consumer(BlockingQueue queue){
		this.queue = queue;
		ha = new HandleA();
		hb = new HandleB();
	}
	
	@Override
	public void run() {
		while(true){
			try {
				//获取数据
				Message msg = this.queue.take();
//				System.out.println("消费成功，消费数据为id: " + msg.getId());
				String s = execute(msg);
				System.out.println("消息处理后:"+s);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	private String execute(Message msg){
		String s = null;
		switch (msg.getType()) {
		case "0":
			s = ha.execute(msg);
			break;
		case "1":
			s = hb.execute(msg);
			break;
		}
		return s;
	}
	
}
